#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<iostream>
#include<QSqlQuery>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tcpServer = NULL;
    tcpSocket = NULL;
    ui->pushButton_close->setEnabled(false);

    ui->textEdit_duankou->setText("8888");
    sql=new sqlexe;

}

MainWindow::~MainWindow()
{
    delete ui;
    if(sql!=NULL)
    {
        delete sql;
    }

}

void MainWindow::on_pushButton_closewin_clicked()
{

    this->close();
}

void MainWindow::on_pushButton_start_clicked()
{


    tcpServer = new QTcpServer(this);
    quint16 port=mport.toUInt();
    if(ui->textEdit_duankou->toPlainText().isEmpty()==true)
    {
        QMessageBox::information(this,"警告","请输入端口号");
        return;
    }
    ui->pushButton_close->setEnabled(true);
    ui->pushButton_start->setEnabled(false);
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(new_client()));
    port=ui->textEdit_duankou->toPlainText().toUInt();
    tcpServer->listen(QHostAddress::AnyIPv4, port);
    ui->textEdit_lianjiezhuangtai->setText("正在监听"+QString::number(port,10)+"端口");



}

void MainWindow::on_pushButton_sql_clicked()
{
    if(!sql->isOpen)
    {
        QMessageBox::information(this,"警告","数据库未连接");
        return;
    }
    QString sqlline=ui->textEdit_mingling->toPlainText();
    sql->exesql(sqlline);
    QString msg;
    QSqlQuery query=sql->getquery();
    while(query.next())
    {
        msg+=query.value(0).toString()+query.value(1).toString()+query.value(2).toString()+query.value(3).toString()+"\n";
    }
    ui->textBrowser->setText(msg);
    if(tcpSocket!=NULL)
    {
    tcpSocket->write(msg.toUtf8());
    }
}

void MainWindow::on_pushButton_close_clicked()
{
    ui->pushButton_close->setEnabled(false);
    ui->pushButton_start->setEnabled(true);
    ui->textEdit_lianjiezhuangtai->setText("服务器已关闭");

    tcpServer->close();
    delete tcpServer;

}

void MainWindow::new_client()
{
     qDebug()<<"新客户段连接";
     QString zhuangtai;


     tcpSocket= tcpServer->nextPendingConnection();

     zhuangtai+=tcpSocket->peerAddress().toString()+":"+QString::number(tcpSocket->peerPort())+"连接成功";
      ui->textEdit_lianjiezhuangtai->setText(zhuangtai);

     connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(read_client_data()));
     connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(client_dis()));

}

void MainWindow::read_client_data()
{
    QTcpSocket *obj = (QTcpSocket*)sender();
    QString msg = obj->readAll();
    if(!sql->isOpen)
    {
        QMessageBox::information(this,"警告","数据库未连接");
        return;
    }
    QString sqlline=msg;
    sql->exesql(sqlline);
    QSqlQuery query=sql->getquery();
    QString msgout;
    while(query.next())
    {
        msgout+=query.value(0).toString()+query.value(1).toString()+query.value(2).toString()+query.value(3).toString()+"\n";
    }
    ui->textBrowser->setText(msgout);
    if(tcpSocket!=NULL)
    {
    tcpSocket->write(msgout.toUtf8());
    }


}

void MainWindow::client_dis()
{
    QTcpSocket *obj = (QTcpSocket*)sender();//掉线对象

    QString msg=obj->peerAddress().toString()+"已断开";
    if(ui->textEdit_lianjiezhuangtai->toPlainText()!="服务器已关闭")
    {
    ui->textEdit_lianjiezhuangtai->setText(msg);
}

}
